﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation; 
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.Xml;
using System.Xml.Serialization;
using System.Data.Services.Client;
using Social_Photos.Social_Photos_Services;
using System.Xml.Linq;
using System.IO;
using System.Windows.Navigation;
using Microsoft.Xna.Framework.Media;
using System.Windows.Media.Imaging;

namespace Social_Photos
{
    public partial class TopView : PhoneApplicationPage
    {
        // Set the string of your streaming service implementation.
        static string serviceUriString = UrlConfig.SERVICE_URI_STRING;

        // Declare the service root URI.
        private Uri svcRootUri =
            new Uri(serviceUriString, UriKind.Absolute);

        // Declare our private binding collection.
        private DataServiceCollection<Photo> _photos;

        // Declare the DataServiceContext as private.
        private SocialPhotoModelContainer _context;
        int i = 0;
        public TopView()
        {
            InitializeComponent();
            login.Visibility = System.Windows.Visibility.Visible;

        }
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            this.LoadDataTop6();
        }
        private void LoadDataTop6()
        {
           
            // Display the progress bar.
            this.requestProgress.Visibility = Visibility.Visible;
            this.requestProgress.IsIndeterminate = true;

            // Load data from the data service.
            _context = new SocialPhotoModelContainer(svcRootUri);
            _photos = new DataServiceCollection<Photo>(_context);

            // Load the data from the Photo feed.
            _photos.LoadAsync(new Uri(UrlConfig.RANDOM_SERVICE_STRING, UriKind.Relative));

            // Register for the LoadCompleted event to turn off the progress bar.
            _photos.LoadCompleted +=
                new EventHandler<LoadCompletedEventArgs>(Photos_LoadCompleted);
        }

        private void Photos_LoadCompleted(object sender, LoadCompletedEventArgs e)
        {
            if (e.Error == null)
            {
                // Make sure that we load all pages of the Photo feed.
                if (_photos.Continuation != null)
                {
                    // Request the next page from the data service.
                    _photos.LoadNextPartialSetAsync();
                }
                else
                {
                    // Load Thumbnail Photos
                    List<TopPhotosListBox> photoList = new List<TopPhotosListBox>();
                    for (int i = 0; i < _photos.Count; i++)
                    {
                        Photo info = _photos[i];
                        Uri uri = new Uri(UrlConfig.GET_THUMBNAIL+ info.FileName);
                        photoList.Add(new TopPhotosListBox(uri.ToString(), info.FileName, info.Id,info.Likes,info.Dislikes,info.Latitude,info.Longitude));
                    }
                    //put image
                    PhotosListBox.ItemsSource = photoList;

                }
                this.requestProgress.Visibility = Visibility.Collapsed;
                this.requestProgress.IsIndeterminate = false;
            }
            else
            {
                if (MessageBox.Show(e.Error.Message, "Retry request?",
                    MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                {
                //    this.LoadData();
                }
            }
        }

     
        private void login_Click(object sender, RoutedEventArgs e)
        {
            if (i == 1) {
                NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
            }else
            myDialog.Visibility = System.Windows.Visibility.Visible;
            //login.Visibility = System.Windows.Visibility.Collapsed;
           
        }

        private void login1_Click(object sender, RoutedEventArgs e)
        {
            myDialog.Visibility = System.Windows.Visibility.Collapsed;
            i = 1;
            NavigationService.Navigate(new Uri("/MainPage.xaml", UriKind.RelativeOrAbsolute));
            
        }


    }
    public class TopPhotosListBox
    {
        public string image { get; set; }
        public string filename { get; set; }
        public int id { get; set; }
        public short? like { get; set; }
        public short? dislike { get; set; }
        public double? latitude { get; set; }
        public double? longitude { get; set; }
        public TopPhotosListBox(string Image, string FileName, int Id, short? Like, short? Dislike, double? Latitude, double? Longitude)
        {
            this.image = Image;
            this.filename = FileName;
            this.id = Id;
            this.like = Like;
            this.dislike = Dislike;
            this.latitude = Latitude;
            this.longitude = Longitude;
        }

    }

    public class MyPhotoListBox
    {
        public string image { get; set; }
        public string filename { get; set; }
        public int id { get; set; }
        public short? like { get; set; }
        public short? dislike { get; set; }
        public double? latitude { get; set; }
        public double? longitude { get; set; }
        public MyPhotoListBox(string Image, string FileName, int Id, short? Like, short? Dislike, double? Latitude, double? Longitude)
        {
            this.image = Image;
            this.filename = FileName;
            this.id = Id;
            this.like = Like;
            this.dislike = Dislike;
            this.latitude = Latitude;
            this.longitude = Longitude;
        }

    }
}